home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Portable Patmos 1.1 / patmos-src / src / progress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-31  |  11.2 KB  |  552 lines  |  [TEXT/KAHL]

  1. #ifdef THINK_C
  2. #define start main
  3. #endif
  4.  
  5. #include <string.h>
  6.  
  7. #include <Files.h>
  8. #include <AppleEvents.h>
  9. #include <Quickdraw.h>
  10. #include <Menus.h>
  11. #include <Windows.h>
  12. #include <ToolUtils.h>
  13. #include <Desk.h>
  14. #include <SegLoad.h>
  15. #include <OSUtils.h>
  16. #include <Memory.h>
  17. #include <Fonts.h>
  18. #include <Events.h>
  19. #include <OSEvents.h>
  20. #include <TextEdit.h>
  21. #include <Dialogs.h>
  22. #include <Aliases.h>
  23. #include <Folders.h>
  24. #include <Errors.h>
  25. #include <GestaltEqu.h>
  26. #include <StandardFile.h>
  27.  
  28. OSErr GotRequiredParams(AppleEvent *);
  29. void scanfolder(FSSpec *);
  30. void main(void);
  31. void SetUpMenus(void);
  32. void AdjustMenus(void);
  33. void HandleMenu(long);
  34. void SetUpWindow(void);
  35. void InitMacintosh(void);
  36. void HandleMouseDown(EventRecord *);
  37. void HandleEvent(void);
  38. void    UpdateFileName(FSSpec *name);
  39. void BeginProcessing(void);
  40. void EndProcessing(void);
  41. void ProcessDoc (FSSpec *curDocFSS);
  42. void HandleMenu (long mSelect);
  43. void SetUpMenus(void);
  44. void InitMacintosh(void);
  45. void HandleMouseDown (EventRecord *theEvent);
  46. void HandleEvent(void);
  47. void SetUpWindow(void);
  48. void ChkOsErr(OSErr);
  49.  
  50. int debug = 0;
  51.  
  52. TEHandle    TEH;
  53. #ifdef PROGRESSWINDOW
  54. WindowPtr    progressWindow;
  55. Rect        dragRect;
  56. Rect        windowBounds = { 40, 40, 200, 300 };
  57. #endif
  58.  
  59. void ChkOsErr(OSErr errcode)
  60.     {
  61.     char errno[9],*p = &errno[sizeof(errno)];
  62.     if (errcode < 0)
  63.         {
  64.         errcode = -errcode;
  65.         while (errcode > 0)
  66.             {
  67.             *--p = errcode%10+'0';
  68.             errcode /= 10;
  69.             }
  70.         *--p = '-';
  71.         *--p = &errno[sizeof(errno)]-p;
  72.         ParamText((void *)p, 0, 0, 0);
  73.         Alert(129, 0);
  74.         ExitToShell();
  75.         }
  76.     }
  77.  
  78. OSErr GotRequiredParams (AppleEvent *theEvent)
  79.     {
  80.     DescType    returnedType;
  81.     long            actualSize;
  82.     OSErr        err;
  83.     
  84.     err = AEGetAttributePtr (theEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, NULL, 0, &actualSize);
  85.     if (err == errAEDescNotFound)
  86.         return noErr;
  87.     else if (err == noErr)
  88.         return errAEEventNotHandled;
  89.     else
  90.         return err;
  91.     }
  92.  
  93. void HandleOapp (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
  94.     {
  95.     errplace = noErr;
  96. #ifdef THINK_C
  97.     asm {
  98.     UNLK      A6
  99.     MOVEA.L   (A7)+,A0
  100.     LEA       12(A7),A7
  101.     JMP       (A0)
  102.     }
  103. #else
  104.     asm ("\n"
  105.     "unlk      a6\n"
  106.     "movel   a7@+,a0\n"
  107.     "lea       a7@(12),a7\n"
  108.     "jmp       a0@\n"
  109.     );    
  110. #endif
  111.     }
  112.  
  113. void HandleOdoc (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr err)
  114.     {
  115.     AEDescList        docList;
  116.     
  117.     err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList, &docList);
  118.     if (err == noErr) 
  119.         {
  120.         err = GotRequiredParams (theAppleEvent);
  121.         if (err == noErr)
  122.             {
  123.             long            numDocs, i, actualSize;
  124.             Boolean        wasAlias,isFolder;
  125.             OSErr        err;
  126.             AEKeyword    keyword;
  127.             DescType    returnedType;
  128.     
  129.             err = AECountItems (&docList, &numDocs);
  130.             if (err == noErr) 
  131.                 {
  132.                 BeginProcessing ();
  133.                 for (i = 1; i <= numDocs; i++) 
  134.                     {
  135.                     FSSpec curDocFSS;
  136.                     err = AEGetNthPtr (&docList, i, typeFSS, &keyword, &returnedType,
  137.                                 (Ptr) &curDocFSS, sizeof (FSSpec), &actualSize);
  138.                     if (err == noErr) 
  139.                         {
  140.                         err = ResolveAliasFile (&curDocFSS, true, &isFolder, &wasAlias);
  141.                         if (err == noErr) 
  142.                             {
  143.                             if (isFolder) scanfolder(&curDocFSS);
  144.                             else ProcessDoc(&curDocFSS);
  145.                             }
  146.                         }
  147.                     }
  148.                 err = noErr;
  149.                 EndProcessing ();
  150.                 }
  151.             }
  152.         (void) AEDisposeDesc (&docList);
  153.         }
  154. #ifdef THINK_C
  155.     asm {
  156.     UNLK      A6
  157.     MOVEA.L   (A7)+,A0
  158.     LEA       12(A7),A7
  159.     JMP       (A0)
  160.     }
  161. #else
  162.     asm ("\n"
  163.     "unlk      a6\n"
  164.     "movel   a7@+,a0\n"
  165.     "lea       a7@(12),a7\n"
  166.     "jmp       a0@\n"
  167.     );    
  168. #endif
  169.     }
  170.  
  171. void HandlePdoc (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
  172. {
  173.     errplace = errAEEventNotHandled;
  174. #ifdef THINK_C
  175.     asm {
  176.     UNLK      A6
  177.     MOVEA.L   (A7)+,A0
  178.     LEA       12(A7),A7
  179.     JMP       (A0)
  180.     }
  181. #else
  182.     asm ("\n"
  183.     "unlk      a6\n"
  184.     "movel   a7@+,a0\n"
  185.     "lea       a7@(12),a7\n"
  186.     "jmp       a0@\n"
  187.     );    
  188. #endif
  189. }
  190.  
  191. void HandleQuit (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
  192. {
  193.     _exit(0);
  194.     errplace = noErr;
  195. #ifdef THINK_C
  196.     asm {
  197.     UNLK      A6
  198.     MOVEA.L   (A7)+,A0
  199.     LEA       12(A7),A7
  200.     JMP       (A0)
  201.     }
  202. #else
  203.     asm ("\n"
  204.     "unlk      a6\n"
  205.     "movel   a7@+,a0\n"
  206.     "lea       a7@(12),a7\n"
  207.     "jmp       a0@\n"
  208.     );    
  209. #endif
  210. }
  211.  
  212. void ReturnEventNotHandled (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
  213. {
  214.     errplace = errAEEventNotHandled;
  215. #ifdef THINK_C
  216.     asm {
  217.     UNLK      A6
  218.     MOVEA.L   (A7)+,A0
  219.     LEA       12(A7),A7
  220.     JMP       (A0)
  221.     }
  222. #else
  223.     asm ("\n"
  224.     "unlk      a6\n"
  225.     "movel   a7@+,a0\n"
  226.     "lea       a7@(12),a7\n"
  227.     "jmp       a0@\n"
  228.     );    
  229. #endif
  230. }
  231.  
  232. OSErr   CurrentProcessLocation(FSSpec *applicationSpec)
  233. {
  234.     ProcessSerialNumber currentPSN;
  235.     ProcessInfoRec info;
  236.     
  237.     currentPSN.highLongOfPSN = 0;
  238.     currentPSN.lowLongOfPSN = kCurrentProcess;
  239.     info.processInfoLength = sizeof(ProcessInfoRec);
  240.     info.processName = NULL;
  241.     info.processAppSpec = applicationSpec;
  242.     return ( GetProcessInformation(¤tPSN, &info) );
  243. }
  244.  
  245. void test_standalone(void)
  246.         {
  247.         FSSpec applicationSpec;
  248.         OSErr err = CurrentProcessLocation(&applicationSpec);
  249.         CInfoPBRec  cPB;
  250.         cPB.hFileInfo.ioNamePtr = applicationSpec.name;
  251.         cPB.hFileInfo.ioVRefNum = applicationSpec.vRefNum;
  252.         cPB.hFileInfo.ioDirID = applicationSpec.parID;
  253.         cPB.hFileInfo.ioFDirIndex = 0;
  254.  
  255.         if (!PBGetCatInfoSync(&cPB))
  256.             {
  257.             long size = cPB.hFileInfo.ioFlLgLen;
  258.             if (size)
  259.                 {
  260.                   BeginProcessing();
  261.                   ProcessDoc(&applicationSpec);
  262.                   EndProcessing();
  263.                 }
  264.             }
  265.         }
  266.  
  267. extern long *system_vectors;
  268.     
  269. void start(void)
  270.     {
  271. //    test_qd();
  272.     InitMacintosh();
  273.     probe();
  274.     SetUpMenus();
  275. /* maintain compatibility with non universal headers */
  276. #ifndef NewAEEventHandlerProc
  277. #define NewAEEventHandlerProc(x) x
  278. #endif
  279.  
  280.     AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOapp), 0, false);
  281.     AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(HandleOdoc), 0, false);
  282.     AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(HandlePdoc), 0, false);
  283.     AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuit), 0, false);
  284.  
  285.     test_standalone();
  286. #ifdef PROGRESSWINDOW
  287.     SetUpWindow();
  288. #endif
  289.     system_vectors[32] = (long)trap_patch_0();
  290.     asm    { 
  291.         moveq    #0,d0
  292.         trap #0
  293.         };
  294. }
  295. /* end main */
  296.  
  297.  
  298.  
  299. MenuHandle    appleMenu, fileMenu, editMenu;
  300.  
  301. enum    {
  302.     appleID = 1,
  303.     fileID,
  304.     editID
  305.     };
  306.  
  307. enum    {
  308.     openItem = 1,
  309.     closeItem,
  310.     quitItem = 4
  311.     };
  312.  
  313. void SetUpMenus(void)
  314. {
  315.     InsertMenu(appleMenu = NewMenu(appleID, (unsigned char *)"\001\024"), 0);
  316.     InsertMenu(fileMenu = NewMenu(fileID, (unsigned char *)"\004File"), 0);
  317.     DrawMenuBar();
  318.     AppendMenu(appleMenu, (unsigned char *)"\011About ...");
  319.     AddResMenu(appleMenu, 'DRVR');
  320.     AppendMenu(fileMenu, (unsigned char *)"\pOpen/O;Flags/L;Quit/Q;Toggle debug/D");
  321. }
  322. /* end SetUpMenus */
  323.  
  324. void HandleMenu(long mSelect)
  325. {
  326.     int            menuID = HiWord(mSelect);
  327.     int            menuItem = LoWord(mSelect);
  328.     Str255        name;
  329.     GrafPtr        savePort;
  330.     WindowPeek    frontWindow;
  331.     
  332.     switch (menuID)
  333.       {
  334.       case    appleID:
  335.           switch(menuItem)
  336.               {
  337.               case 1:
  338.               Alert(199,0);
  339.               break;
  340.               default:
  341.             GetPort(&savePort);
  342.             GetItem(appleMenu, menuItem, name);
  343.             OpenDeskAcc(name);
  344.             SetPort(savePort);
  345.             }
  346.         break;
  347.     
  348.       case    fileID:
  349.           switch(menuItem)
  350.               {
  351.               case 1: 
  352.                   {
  353.                 long        aProcID;
  354.                   SFReply macSFReply;
  355.                   FSSpec new;
  356.                 Point    where;
  357.                 SFTypeList    typeList;
  358.             
  359.                 typeList[0]='TEXT';    /* file type to search for */
  360.                 typeList[1]='ŎŎ';    /* file type to search for */
  361.                 where.h=20;  where.v=90;    /* SF dialog window position */
  362.             
  363.                 SFGetFile(where,        /* put topLeft corner of dialog box here */
  364.                             (ConstStr255Param)0,            /* always 0 on SFGetFile() */
  365.                             (ProcPtr)0,            /* address of custom filter */
  366.                             -1,            /* looking for all file types */
  367.                             typeList,    /* start of list of file types */
  368.                             (ProcPtr)0,     /* use standard dialog handler */
  369.                             &macSFReply);    /* put results here */
  370.  
  371.                 if ( macSFReply.good ) 
  372.                     { 
  373.                     WDPBRec    wdpb;
  374.                     wdpb.ioNamePtr = 0;
  375.                     wdpb.ioVRefNum = macSFReply.vRefNum;
  376.                     wdpb.ioWDIndex =0;
  377.                     wdpb.ioWDProcID =0;
  378.                     wdpb.ioWDVRefNum =0;
  379.                     PBGetWDInfoSync( &wdpb );
  380.                     new.parID = wdpb.ioWDDirID;
  381.                     new.vRefNum = wdpb.ioWDVRefNum;
  382.                       BlockMove(macSFReply.fName, new.name, macSFReply.fName[0]+1);
  383.                       BeginProcessing();
  384.                       ProcessDoc(&new);
  385.                       EndProcessing();
  386.                     }
  387.                   }
  388.               break;
  389.             case 2: 
  390.                 {
  391. //                crt_getargs();
  392.                 break;
  393.                 }
  394.             case 3: _exit(0);
  395.             case 4: debug = !debug; break;
  396.             }
  397.         break;
  398.                   
  399.       }
  400. }
  401. /* end HandleMenu */
  402.  
  403. #ifdef PROGRESSWINDOW
  404.  
  405. void SetUpWindow(void)
  406.  
  407. {
  408.     dragRect = qd.screenBits.bounds;
  409.     
  410.     progressWindow = NewWindow(0L, &windowBounds, (unsigned char *)"\010progress", 
  411.         true, noGrowDocProc, (WindowPtr) -1L, true, 0);
  412.     SetPort(progressWindow);
  413.     TEH = TENew(&windowBounds, &windowBounds);
  414. }
  415. /* end SetUpWindow */
  416.  
  417. #endif
  418.  
  419.  
  420. void InitMacintosh(void)
  421.  
  422. {
  423.     MaxApplZone();
  424.     
  425.     InitGraf(&qd.thePort);
  426.     InitFonts();
  427.     FlushEvents(everyEvent, 0);
  428.     InitWindows();
  429.     InitMenus();
  430.     TEInit();
  431.     InitDialogs(0L);
  432.     InitCursor();
  433.  
  434. }
  435. /* end InitMacintosh */
  436.  
  437. #if 0
  438.  
  439. void HandleMouseDown (EventRecord    *theEvent)
  440.  
  441. {
  442.     WindowPtr    theWindow;
  443.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  444.     
  445.     switch (windowCode)
  446.       {
  447.       case inSysWindow: 
  448.         SystemClick (theEvent, theWindow);
  449.         break;
  450.         
  451.       case inMenuBar:
  452.         HandleMenu(MenuSelect(theEvent->where));
  453.         break;
  454.  
  455. #ifdef PROGRESSWINDOW
  456.         
  457.       case inDrag:
  458.           if (theWindow == progressWindow)
  459.             DragWindow(progressWindow, theEvent->where, &dragRect);
  460.             break;
  461.             
  462.       case inContent:
  463.           if (theWindow == progressWindow)
  464.             {
  465.             if (theWindow != FrontWindow())
  466.               SelectWindow(progressWindow);
  467.             else
  468.               InvalRect(&progressWindow->portRect);
  469.             }
  470.           break;
  471.           
  472.       case inGoAway:
  473.           if (theWindow == progressWindow && 
  474.               TrackGoAway(progressWindow, theEvent->where))
  475.           HideWindow(progressWindow);
  476.             break;
  477. #endif
  478.       }
  479. }
  480. /* end HandleMouseDown */
  481.  
  482. void HandleEvent(void)
  483. {
  484.     int            ok;
  485.     EventRecord    theEvent;
  486.  
  487.     HiliteMenu(0);
  488.     SystemTask ();        /* Handle desk accessories */
  489.     
  490.     ok = GetNextEvent (everyEvent, &theEvent);
  491.     if (ok)
  492.       switch (theEvent.what)
  493.         {
  494.         case mouseDown:
  495.             HandleMouseDown(&theEvent);
  496.             break;
  497.             
  498.         case keyDown: 
  499.         case autoKey:
  500.             if ((theEvent.modifiers & cmdKey) != 0)
  501.               {
  502.               HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  503.               }
  504.             break;
  505.  
  506. #ifdef PROGRESSWINDOW
  507.             
  508.         case updateEvt: if (progressWindow)
  509.             {
  510.             Rect upper = progressWindow->portRect;
  511.             Rect lower = progressWindow->portRect;
  512.             upper.bottom = upper.top + 32;
  513.             lower.top = upper.bottom;
  514.             BeginUpdate(progressWindow);
  515.             SetPort(progressWindow);
  516.             EraseRect(&lower);
  517.             upper.right = lower.right;
  518.             FillRect(&upper,(void *)&qd.white);
  519.             TEUpdate(&progressWindow->portRect, TEH);
  520.             EndUpdate(progressWindow);
  521.             break;
  522.             }
  523.         case activateEvt:
  524.             InvalRect(&progressWindow->portRect);
  525.             break;
  526. #endif
  527.  
  528.         case kHighLevelEvent:
  529.             AEProcessAppleEvent (&theEvent);
  530.             break;
  531.         }
  532. }
  533. /* end HandleEvent */
  534.  
  535. #endif
  536. #ifdef PROGRESSWINDOW
  537.  
  538. void    UpdateFileName(FSSpec *new)
  539.     {
  540.     if (progressWindow)
  541.         {
  542.         Rect upper = progressWindow->portRect;
  543.         Rect lower = progressWindow->portRect;
  544.         upper.bottom = upper.top + 32;
  545.         lower.top = upper.bottom;
  546.         TESetText(&(new->name[1]),new->name[0],TEH);
  547.         InvalRect(&lower);
  548.         HandleEvent();
  549.         }
  550.     }
  551.  
  552. #endif